package com.example.warehouse.bus.service.impl;

import com.example.warehouse.bus.dao.persist.repository.IBusInportRepository;
import com.example.warehouse.bus.pojo.entity.BusInport;
import com.example.warehouse.bus.pojo.param.BusInportAddNewParam;
import com.example.warehouse.bus.pojo.param.BusInportUpdateParam;
import com.example.warehouse.bus.pojo.vo.BusInportListItemVO;
import com.example.warehouse.bus.service.IBusInportService;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.security.CurrentPrincipal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class IBusInportServiceImpl implements IBusInportService {

    /*默认的查询每页条目数大小*/
    @Value("${warehouse.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    IBusInportRepository repository;

    /**
     * 新增进货--增
     * @param busInportAddNewParam
     * @param currentPrincipal
     */
    @Override
    public int insert(BusInportAddNewParam busInportAddNewParam, CurrentPrincipal currentPrincipal) {
      log.debug("开始处理[添加进货]的业务,参数类:{},当事人对象:{}",busInportAddNewParam,currentPrincipal);

        BusInport busInport = new BusInport();
        BeanUtils.copyProperties(busInportAddNewParam,busInport);

        String opman= currentPrincipal.getUsername();
        busInport.setOperatePerson(opman);

        return repository.insert(busInport);
    }

    /**
     * 根据id删除进货数据--删
     * @param id
     * @param currentPrincipal 需要删除的数据的当事人信息
     * @return
     */
    @Override
    public int deleteById(Long id,CurrentPrincipal currentPrincipal) {
        log.debug("开始处理[根据id删除进货数据--删]的业务,参数:{}",currentPrincipal);

        return repository.deleteById(id,currentPrincipal);
    }


    /**
     * 根据id修改数据--改
     * @param busInportUpdateParam
     * @return
     */
    @Override
    public int updateById(BusInportUpdateParam busInportUpdateParam) {
        log.debug("开始处理[根据id修改进货数据--改]的业务,参数:{}",busInportUpdateParam);

        return repository.updateById(busInportUpdateParam);
    }

    /**
     * 进货数据列表--查
     * 默认5页:defaultQueryPageSize
     * @param pageNum 页码
     * @return 进货列表
     */
    @Override
    public PageData<BusInportListItemVO> list(Integer pageNum) {
        log.debug("开始处理【查询进货列表】的业务，页码：{}", pageNum);
       PageData<BusInportListItemVO> pageData= repository.list(pageNum,defaultQueryPageSize);
       return pageData;
    }

    /**
     * 进货数据列表--查
     * 根据用户输入的每页数据数:pageSize
     * @param pageNum 页码
     * @param pageSize 每页记录数
     * @return
     */
    @Override
    public PageData<BusInportListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询进货列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<BusInportListItemVO> pageData=repository.list(pageNum,pageSize);
        return pageData;
    }
}
